package com.wrb.pwd.component;

import com.wrb.pwd.model.User;
import com.wrb.pwd.model.ValidationQuestion;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wrb
 * @date 2019/10/8 14:48
 */
public class UserShowDialog extends JDialog {

	public UserShowDialog(Component parentComponent, User user) {
		super((Frame) null, "用户详细信息", true);

		// 设置对话框的宽高
		this.setSize(300, 550);
		// 设置对话框大小不可改变
		this.setResizable(false);
		// 设置对话框相对显示的位置
		this.setLocationRelativeTo(parentComponent);

		// 创建标签显示消息内容
		JLabel lbPlatform = new JLabel("平   台");
		JLabel lbName = new JLabel("用户名");
		JLabel lblPwd = new JLabel("密   码");
		JLabel lbPhone = new JLabel("手机号码");
		JLabel lbEmail = new JLabel("邮   箱");
		JLabel lbAuxEmail = new JLabel("辅助邮箱");
		JLabel lbRemark = new JLabel("备   注");
		JLabel lbCreateTime = new JLabel("创建时间");
		JLabel lbUpdateTime = new JLabel("更新时间");
		JLabel lbQue1 = new JLabel("问 题 1");
		JLabel lbAns1 = new JLabel("答 案 1");
		JLabel lbQue2 = new JLabel("问 题 2");
		JLabel lbAns2 = new JLabel("答 案 2");
		JLabel lbQue3 = new JLabel("问 题 3");
		JLabel lbAns3 = new JLabel("答 案 3");

		JTextField txtPlatform = new JTextField(20);
//		txtPlatform.setEditable(false);
		JTextField txtName = new JTextField(20);
		JTextField txtPwd = new JTextField(20);
//		txtPwd.setEditable(false);
		JTextField txtPhone = new JTextField(20);
		JTextField txtEmail = new JTextField(20);
		JTextField txtAuxEmail = new JTextField(20);
		JTextField txtRemark = new JTextField(20);
		JTextField txtCreateTime = new JTextField(20);
		JTextField txtUpdateTime = new JTextField(20);
		JTextField txtAns1 = new JTextField(20);
		JTextField txtQue1 = new JTextField(20);
		JTextField txtAns2 = new JTextField(20);
		JTextField txtQue2 = new JTextField(20);
		JTextField txtAns3 = new JTextField(20);
		JTextField txtQue3 = new JTextField(20);

		//设置初始值
		txtPlatform.setText(user.getPlatform());
		txtName.setText(user.getName());
		txtPwd.setText(user.getPassword());
		txtPhone.setText(user.getPhone());
		txtEmail.setText(user.getEmail());
		txtAuxEmail.setText(user.getAuxiliaryEmail());
		txtRemark.setText(user.getRemark());
		txtCreateTime.setText(dayAndTime(user.getCreateTime()));
		txtUpdateTime.setText(dayAndTime(user.getUpdateTime()));
		try {
			List<ValidationQuestion> list = user.getValidationQuestionList();
			txtQue1.setText(list.get(0).getQuestion());
			txtAns1.setText(list.get(0).getAnswer());
			txtQue2.setText(list.get(1).getQuestion());
			txtAns2.setText(list.get(1).getAnswer());
			txtQue3.setText(list.get(2).getQuestion());
			txtAns3.setText(list.get(2).getAnswer());
		} catch (Exception ignored) {
		}


		// 创建一个按钮用于关闭对话框
		JButton okBtn = new JButton("关闭");
		okBtn.addActionListener(e -> this.dispose());

		lbPlatform.setBounds(20, 20, 60, 25);
		txtPlatform.setBounds(85, 20, 180, 25);
		lbName.setBounds(20, 50, 60, 25);
		txtName.setBounds(85, 50, 180, 25);
		lblPwd.setBounds(20, 80, 60, 25);
		txtPwd.setBounds(85, 80, 180, 25);
		lbPhone.setBounds(20, 110, 60, 25);
		txtPhone.setBounds(85, 110, 180, 25);
		lbEmail.setBounds(20, 140, 60, 25);
		txtEmail.setBounds(85, 140, 180, 25);
		lbAuxEmail.setBounds(20, 170, 60, 25);
		txtAuxEmail.setBounds(85, 170, 180, 25);
		lbRemark.setBounds(20, 200, 60, 25);
		txtRemark.setBounds(85, 200, 180, 25);
		lbCreateTime.setBounds(20, 230, 60, 25);
		txtCreateTime.setBounds(85, 230, 180, 25);
		lbUpdateTime.setBounds(20, 260, 60, 25);
		txtUpdateTime.setBounds(85, 260, 180, 25);
		lbQue1.setBounds(20, 290, 60, 25);
		txtQue1.setBounds(85, 290, 180, 25);
		lbAns1.setBounds(20, 320, 60, 25);
		txtAns1.setBounds(85, 320, 180, 25);
		lbQue2.setBounds(20, 350, 60, 25);
		txtQue2.setBounds(85, 350, 180, 25);
		lbAns2.setBounds(20, 380, 60, 25);
		txtAns2.setBounds(85, 380, 180, 25);
		lbQue3.setBounds(20, 410, 60, 25);
		txtQue3.setBounds(85, 410, 180, 25);
		lbAns3.setBounds(20, 440, 60, 25);
		txtAns3.setBounds(85, 440, 180, 25);

		okBtn.setBounds(115, 480, 60, 25);

		// 创建对话框的内容面板, 在面板内可以根据自己的需要添加任何组件并做任意是布局
		JPanel panel = new JPanel(null);

		// 添加组件到面板
		panel.add(lbPlatform);
		panel.add(txtPlatform);
		panel.add(lbName);
		panel.add(txtName);
		panel.add(lblPwd);
		panel.add(txtPwd);
		panel.add(lbPhone);
		panel.add(txtPhone);
		panel.add(lbEmail);
		panel.add(txtEmail);
		panel.add(lbAuxEmail);
		panel.add(txtAuxEmail);
		panel.add(lbRemark);
		panel.add(txtRemark);
		panel.add(lbCreateTime);
		panel.add(txtCreateTime);
		panel.add(lbUpdateTime);
		panel.add(txtUpdateTime);
		panel.add(okBtn);
		panel.add(lbAns1);
		panel.add(lbQue1);
		panel.add(txtAns1);
		panel.add(txtQue1);
		panel.add(lbAns2);
		panel.add(lbQue2);
		panel.add(txtAns2);
		panel.add(txtQue2);
		panel.add(lbAns3);
		panel.add(lbQue3);
		panel.add(txtAns3);
		panel.add(txtQue3);

		// 设置对话框的内容面板
		this.setContentPane(panel);
		//设置模式 dialog关闭后才能获取state的值
		this.setModal(true);
		// 显示对话框
		this.setVisible(true);
	}

	/**
	 * Date 转 字符串日期
	 *
	 * @param date
	 * @return
	 */
	public String dayAndTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(date);
	}
}
